home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Mac OS Development Toolkit / Automation Essentials 2.3.0 / Host Automation Folder / VU External Tool libs / FileTool.vuLib < prev    next >
Encoding:
Text File  |  1998-03-19  |  17.2 KB  |  535 lines  |  [TEXT/MPS ]

  1. #
  2. #    File:        FileTool.vulib
  3. #
  4. #    Contains:    The tool definition along with the high level tasks to access
  5. #                the tool's services
  6. #
  7. #    Written by:    P. Nagarajan
  8. #
  9. #    Copyright:    © 1993-1997 by Apple Computer, Inc., all rights reserved.
  10. #
  11. #    Change History (most recent first):
  12. #
  13. #        2.2.2    02/04/97    JAS        Added 'vers' resources to library.
  14. #                                    These should always match tool version when tool is revved.
  15. #                02/04/97    JAS        Added task InitFindFolderGlobals()
  16. #         <5>    10/23/96    JAS        Updated Service descriptions to match FileTool docs
  17. #         <4>    07/23/96    SBR        removed StandardServices
  18. #         <3>    07/06/96    SBR        added StandardServices from ThreadedVUTool template
  19. #         <2>    2/1/95        RV        change FileExists return value from integer to boolean symbol
  20. #                3/26/93        NAGA    xxx put comment here xxx
  21. #
  22. #    To Do:
  23. #
  24.  
  25. tool FileTool s:'FILE'
  26. begin
  27.     # Services specific to FileTool:
  28.     
  29.     # To create a file
  30.     Service    "Create"( 'string', 'string', 'string' ) return 'integer';
  31.     # First parameter is the full pathname for the file 
  32.     # Second parameter is the 4 character creator type ( eg., 'MPS ') 
  33.     # Third parameter is the 4 character file type (eg., 'TEXT') 
  34.     # Return value is the file reference number for future calls
  35.     
  36.     # To create a folder
  37.     Service    "CreateFolder"( 'string' ) return 'integer';
  38.     # First parameter is the full pathname for the file 
  39.     # Return value is the file reference number for future calls
  40.     
  41.     # To delete a file/folder
  42.     Service    "Delete"( 'string', 'integer' );
  43.     # First parameter is the full pathname for the file (can be '', if second
  44.     # parameter is given non zero value)
  45.     # Second parameter if non-zero is the file reference previously 
  46.     # returned by the tool
  47.     # Once a file/folder is deleted its reference number is no longer valid
  48.  
  49.     # To erase the contents of the data fork of a file
  50.     Service    "Erase"( 'string', 'integer' ) return 'integer';
  51.     # First parameter is the full pathname for the file (can be '', if second
  52.     # parameter is given non zero value)
  53.     # Second parameter if non-zero is the file reference previously 
  54.     # returned by the tool
  55.  
  56.     # To check if a file exists
  57.     Service    "FileExists"( 'string' ) return 'symbol';
  58.     # First parameter is the full pathname for the file (can be '', if second
  59.     # parameter is given non zero value)
  60.     # Return value is 1 if file exists else 0
  61.  
  62.     # To copy a file
  63.     Service    "CopyFile"( 'string', 'integer', 'string' ) return 'integer';
  64.     # First parameter is the full pathname for the file (can be '', if second
  65.     # parameter is given non zero value)
  66.     # Second parameter if non-zero is the file reference previously 
  67.     # returned by the tool. This can be zero if the first parameter
  68.     # is fully specified.
  69.     # Third parameter is the full pathname for the new file
  70.     # Return value is 1 if file copied else 0
  71.  
  72.     # To get back memory in the FileTool heap
  73.     Service    "ForgetFile"( 'string', 'integer' ) return 'integer';
  74.     # First parameter is the full pathname for the file (can be '', if second
  75.     # parameter is given non zero value)
  76.     # Second parameter if non-zero is the file reference previously 
  77.     # returned by the tool
  78.     # Return value is insignificant
  79.  
  80.     # To move a file/folder
  81.     Service    "Move"( 'string', 'integer', 'string' ) return 'integer';
  82.     # First parameter is the full pathname for the file (can be '', if second
  83.     # parameter is given non zero value)
  84.     # Second parameter if non-zero is the file reference previously 
  85.     # returned by the tool
  86.     # Third parameter is the new file path name
  87.     # Return value is the file reference number for future calls
  88.  
  89.     # To read from a file a specified number of characters
  90.     Service    "Read"( 'string', 'integer', 'integer', 'integer' ) return 'string';
  91.     # First parameter is the full pathname for the file (can be '', if second
  92.     # parameter is given non zero value)
  93.     # Second parameter if non-zero is the file reference previously 
  94.     # returned by the tool
  95.     # Third parameter is the number of characters to be read
  96.     # Fourth parameter is the number of characters from start of file after
  97.     # which to start reading
  98.     # Return value is the string read from the file
  99.  
  100.     # To read from a file until a specified character is seen
  101.     Service    "ReadUntil"( 'string', 'integer', 'string', 'integer' ) return 'string';
  102.     # First parameter is the full pathname for the file (can be '', if second
  103.     # parameter is given non zero value)
  104.     # Second parameter if non-zero is the file reference previously 
  105.     # returned by the tool
  106.     # Third parameter is the single character string that indicates the
  107.     # character until which the contents of a file has to be read
  108.     # Fourth parameter is the number of characters from start of file after
  109.     # which to start reading
  110.     # Return value is the string read from the file
  111.  
  112.     # To read a specific line from a file
  113.     Service    "ReadLine"( 'string', 'integer', 'integer', 'string' ) return 'string';
  114.     # First parameter is the full pathname for the file (can be '', if second
  115.     # parameter is given non zero value)
  116.     # Second parameter if non-zero is the file reference previously 
  117.     # returned by the tool
  118.     # Third parameter is the number of the desired line
  119.     # Fourth parameter is the character that terminates lines, e.g. "\r"
  120.     # Return value is the string read from the file
  121.  
  122.     # To rename a file/folder
  123.     Service    "Rename"( 'string', 'integer', 'string' ) return 'integer';
  124.     # First parameter is the full pathname for the file (can be '', if second
  125.     # parameter is given non zero value)
  126.     # Second parameter if non-zero is the file reference previously 
  127.     # returned by the tool
  128.     # Third parameter is the new file name (not a path name)
  129.     # Return value is the file reference number for future calls
  130.  
  131.     # To get back the list of mounted volume names
  132.     Service    "VolumeList"() return 'list';
  133.     
  134.     # To write to the data fork of a file
  135.     Service    "Write"( 'string', 'integer', 'string' ) return 'integer';
  136.     # First parameter is the full pathname for the file (can be '', if second
  137.     # parameter is given non zero value)
  138.     # Second parameter if non-zero is the file reference previously 
  139.     # returned by the tool
  140.     # Third parameter is the text to be appended to (written at the end
  141.     # of) the file specified by the first two parameters
  142.     # Return value is the file reference number for future calls
  143.  
  144.     # To append the second file to the first, both must be text files
  145.     Service    "AppendFile"( 'string', 'string' ) return 'symbol';
  146.  
  147.     # To locate the special folders defined by the system
  148.     Service    "FindFolder"( 'string' ) return 'string';
  149.     # First parameter is the four character code for the folder, the FolderType
  150.     # The full path name to the folder is returned 
  151.  
  152.     # Read the short & long version string from the specified file
  153.     Service    "ReadVersion"( 'string', 'integer' ) return 'list';
  154.  
  155.     # Test if a resource exists within a specified file
  156.     Service    "ResourceExists"( 'string', 'string', 'integer' ) return 'symbol';
  157.     # Return value is true if the resource exists.
  158.  
  159.     # Read the 'SIZE' resource from an application file
  160.     Service    "ReadSizeResource"( 'string', 'integer' ) return 'list';
  161.     # First parameter is the full pathname of the application
  162.     # Second parameter is the ID of the 'SIZE' resource to be read
  163.     # Returns the preferred and minimum memory partitions (in bytes)
  164.  
  165.     # Services common to all Virtual User external tools:
  166.     Service "Initialize"( 'undefined' ); # pass TRUE for target, FALSE for host
  167.     Service "Cancel"( 'string' );
  168.     Service "GetToolServices"() return 'list';
  169.     Service "GetToolVersion"() return 'list';
  170.     Service "ServiceSupported"( 'string' ) return 'undefined';
  171.     Service "Quit"();
  172.     
  173. end;
  174.  
  175. task CreateFile( pFilePathName, pSignature := 'MPS ', pType := 'TEXT' )
  176. begin
  177.     return FileTool( "Create", pFilePathName, pSignature, pType ); 
  178. end;
  179.  
  180. task CreateFolder( pFolderPathName )
  181. begin
  182.     return FileTool( "CreateFolder", pFolderPathName ); 
  183. end;
  184.  
  185. task DeleteFile( pFileReference )
  186. begin
  187.     if( typeOf( pFileReference ) = 'integer' )
  188.     begin
  189.         tFilePathStr := '';
  190.         tFileReferenceNumber := pFileReference;
  191.     end;
  192.     else if( typeOf( pFileReference ) = 'string' )
  193.     begin
  194.         tFilePathStr := pFileReference;
  195.         tFileReferenceNumber := 0;
  196.     end;
  197.     else
  198.         return -50;#gParamError = -50, same as what the tool returns
  199.     
  200.     # Now call the tool with tFilePathStr, tFileReferenceNumber as parameters
  201.     return FileTool( "Delete", tFilePathStr, tFileReferenceNumber ); 
  202.     
  203. end;
  204.  
  205. # This task will empty the contents of the specified file (dataFork only)
  206. # the file will not be deleted
  207. task EraseFile( pFileReference )
  208. begin
  209.     if( typeOf( pFileReference ) = 'integer' )
  210.     begin
  211.         tFilePathStr := '';
  212.         tFileReferenceNumber := pFileReference;
  213.     end;
  214.     else if( typeOf( pFileReference ) = 'string' )
  215.     begin
  216.         tFilePathStr := pFileReference;
  217.         tFileReferenceNumber := 0;
  218.     end;
  219.     else
  220.         return -50;#gParamError = -50, same as what the tool returns
  221.     
  222.     # Now call the tool with tFilePathStr, tFileReferenceNumber as parameters
  223.     return FileTool( "Erase", tFilePathStr, tFileReferenceNumber ); 
  224.     
  225. end;
  226.  
  227. task FileExists( pFilePathName )
  228. begin
  229.     if( typeOf( pFilePathName ) <> 'string' )
  230.     begin
  231.         return -50;#gParamError = -50, same as what the tool returns
  232.     end;
  233.     
  234.     # Now call the tool with pFilePathName as parameter
  235.     return FileTool( "FileExists", pFilePathName ); 
  236.     
  237. end;
  238.  
  239. task CopyFile( pFileReference, pDestPathName )
  240. begin
  241.     if( typeOf( pFileReference ) = 'integer' )
  242.     begin
  243.         tFilePathStr := '';
  244.         tFileReferenceNumber := pFileReference;
  245.     end;
  246.     else if( typeOf( pFileReference ) = 'string' )
  247.     begin
  248.         tFilePathStr := pFileReference;
  249.         tFileReferenceNumber := 0;
  250.     end;
  251.     else
  252.         return -50;#gParamError = -50, same as what the tool returns
  253.     
  254.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pDestPathName as parameters
  255.     return FileTool( "CopyFile", tFilePathStr, tFileReferenceNumber, pDestPathName ); 
  256.     
  257. end;
  258.  
  259. # This task will free up memory in the FileTool
  260. # the file will not be touched or affected in any way
  261. task ForgetFile( pFileReference )
  262. begin
  263.     if( typeOf( pFileReference ) = 'integer' )
  264.     begin
  265.         tFilePathStr := '';
  266.         tFileReferenceNumber := pFileReference;
  267.     end;
  268.     else if( typeOf( pFileReference ) = 'string' )
  269.     begin
  270.         tFilePathStr := pFileReference;
  271.         tFileReferenceNumber := 0;
  272.     end;
  273.     else
  274.         return -50;#gParamError = -50, same as what the tool returns
  275.     
  276.     # Now call the tool with tFilePathStr, tFileReferenceNumber as parameters
  277.     return FileTool( "ForgetFile", tFilePathStr, tFileReferenceNumber ); 
  278.     
  279. end;
  280.  
  281. task MoveFile( pFileReference, pNewPathName )
  282. begin
  283.     if( typeOf( pFileReference ) = 'integer' )
  284.     begin
  285.         tFilePathStr := '';
  286.         tFileReferenceNumber := pFileReference;
  287.     end;
  288.     else if( typeOf( pFileReference ) = 'string' )
  289.     begin
  290.         tFilePathStr := pFileReference;
  291.         tFileReferenceNumber := 0;
  292.     end;
  293.     else
  294.         return -50;#gParamError = -50, same as what the tool returns
  295.     
  296.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pNewPathName as parameters
  297.     return FileTool( "Move", tFilePathStr, tFileReferenceNumber, pNewPathName ); 
  298.     
  299. end;
  300.  
  301. task RenameFile( pFileReference, pNewName )
  302. begin
  303.     if( typeOf( pFileReference ) = 'integer' )
  304.     begin
  305.         tFilePathStr := '';
  306.         tFileReferenceNumber := pFileReference;
  307.     end;
  308.     else if( typeOf( pFileReference ) = 'string' )
  309.     begin
  310.         tFilePathStr := pFileReference;
  311.         tFileReferenceNumber := 0;
  312.     end;
  313.     else
  314.         return -50;#gParamError = -50, same as what the tool returns
  315.     
  316.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pNewName as parameters
  317.     return FileTool( "Rename", tFilePathStr, tFileReferenceNumber, pNewName ); 
  318.     
  319. end;
  320.  
  321. task VolumeList()
  322. begin
  323.     
  324.     # Now call the tool 
  325.     return FileTool( "VolumeList" ); 
  326.     
  327. end;
  328.  
  329. # a utility task used by WriteToFile
  330. task ToText( pData )
  331. begin
  332.     if( typeOf( pData ) = 'string' ) return pData;
  333.     if( typeOf( pData ) = 'integer' ) return NumToStr( pData );
  334.     return undefined; # else
  335. end;
  336.  
  337. # This task will append pData to end of the specified file
  338. # pData can a string or a list of strings and numbers
  339. task WriteToFile( pFileReference, pData )
  340. begin
  341.     tText := '';
  342.     if( typeOf( pFileReference ) = 'integer' )
  343.     begin
  344.         tFilePathStr := '';
  345.         tFileReferenceNumber := pFileReference;
  346.     end;
  347.     else if( typeOf( pFileReference ) = 'string' )
  348.     begin
  349.         tFilePathStr := pFileReference;
  350.         tFileReferenceNumber := 0;
  351.     end;
  352.     else
  353.         return -50;#gParamError = -50, same as what the tool returns
  354.     
  355.     if( typeOf( pData ) = 'list' )
  356.     begin
  357.         for i := 1 to card pData
  358.         begin
  359.             tNext := ToText( pData[i] );
  360.             if( tNext )
  361.                 tText := tText + tNext;
  362.             else
  363.                 return -50;
  364.         end;
  365.     end;
  366.     else if( typeOf( pData ) = 'string' )
  367.     begin
  368.         tText := pData;
  369.     end;
  370.     else
  371.         return -50;#gParamError = -50, same as what the tool returns
  372.  
  373.     # Now call the tool with tFilePathStr, tFileReferenceNumber and tText as parameters
  374.     return FileTool( "Write", tFilePathStr, tFileReferenceNumber, tText ); 
  375.  
  376. end;
  377.  
  378.  
  379. task Read( pFileReference, pNumOfCharacters, pFromPosition := 0 )
  380. begin
  381.     if( typeOf( pFileReference ) = 'integer' )
  382.     begin
  383.         tFilePathStr := '';
  384.         tFileReferenceNumber := pFileReference;
  385.     end;
  386.     else if( typeOf( pFileReference ) = 'string' )
  387.     begin
  388.         tFilePathStr := pFileReference;
  389.         tFileReferenceNumber := 0;
  390.     end;
  391.     else
  392.         return -50;#gParamError = -50, same as what the tool returns
  393.     
  394.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pNumOfCharacters as parameters
  395.     return FileTool( "Read", tFilePathStr, tFileReferenceNumber, pNumOfCharacters, pFromPosition ); 
  396.     
  397. end;
  398.  
  399. task ReadUntil( pFileReference, pUntilCharacter, pFromPosition := 0 )
  400. begin
  401.     if( typeOf( pFileReference ) = 'integer' )
  402.     begin
  403.         tFilePathStr := '';
  404.         tFileReferenceNumber := pFileReference;
  405.     end;
  406.     else if( typeOf( pFileReference ) = 'string' )
  407.     begin
  408.         tFilePathStr := pFileReference;
  409.         tFileReferenceNumber := 0;
  410.     end;
  411.     else
  412.         return -50;#gParamError = -50, same as what the tool returns
  413.     
  414.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pUntilCharacter as parameters
  415.     return FileTool( "ReadUntil", tFilePathStr, tFileReferenceNumber, pUntilCharacter, pFromPosition ); 
  416.     
  417. end;
  418.  
  419. task ReadLine( pFileReference, pLineNumber := 0, pLineTermChar := "∂n")
  420. begin
  421.     if( typeOf( pFileReference ) = 'integer' )
  422.     begin
  423.         tFilePathStr := '';
  424.         tFileReferenceNumber := pFileReference;
  425.     end;
  426.     else if( typeOf( pFileReference ) = 'string' )
  427.     begin
  428.         tFilePathStr := pFileReference;
  429.         tFileReferenceNumber := 0;
  430.     end;
  431.     else
  432.         return -50;    #gParamError = -50, same as what the tool returns
  433.     
  434.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pUntilCharacter as parameters
  435.     return FileTool( "ReadLine", tFilePathStr, tFileReferenceNumber, 
  436.                             pLineNumber, pLineTermChar ); 
  437.     
  438. end;
  439.  
  440.  
  441.  
  442.  
  443. task AppendFile( pTargetFile, pSourceFile )
  444. begin
  445.     if not ( typeOf( pTargetFile ) = 'string' )
  446.     begin
  447.         return { -50 };#gParamError = -50, same as what the tool returns
  448.     end;
  449.     
  450.     if not ( typeOf( pSourceFile ) = 'string' )
  451.     begin
  452.         return { -50 };#gParamError = -50, same as what the tool returns
  453.     end;
  454.         
  455.     # Now call the tool with tTargetFile, tSourceFile
  456.     x := FileTool( "AppendFile", pTargetFile, pSourceFile ); 
  457.     return x;
  458. end;
  459.  
  460.  
  461. #########################################################################
  462. #    task        InitFindFolderGlobals()
  463. #∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
  464. #    Description:    Initializes global constants for use with FindFolder
  465. #
  466. #    Parameters:        None
  467. #
  468. #    Returns:        Nothing
  469. #
  470. #    Example:        InitFindFolderGlobals()
  471. #
  472. #    Assumptions:    VU 2.1
  473. #∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞
  474. #    History:
  475. #        02/04/97    JAS            created 
  476. #########################################################################
  477. task InitFindFolderGlobals()
  478. begin
  479.     # global constants used by FindFolder:
  480.     
  481.     global kSystemFolderType            := "macs";    # the system folder 
  482.     global kDesktopFolderType            := 'desk';    # the desktop folder; objects in this folder show on the desk top. 
  483.     global kTrashFolderType                := 'trsh';    # the trash folder; objects in this folder show up in the trash 
  484.     global kWhereToEmptyTrashFolderType := 'empt';    # the "empty trash" folder; Finder starts empty from here down 
  485.     global kPrintMonitorDocsFolderType    := 'prnt';    # Print Monitor documents 
  486.     global kStartupFolderType            := 'strt';    # Finder objects (applications, documents, DAs, aliases, to...) to open at startup go here 
  487.     global kShutdownFolderType            := 'shdf';    # Finder objects (applications, documents, DAs, aliases, to...) to open at shutdown go here 
  488.     global kAppleMenuFolderType            := 'amnu';    # Finder objects to put into the Apple menu go here 
  489.     global kControlPanelFolderType        := 'ctrl';    # Control Panels go here (may contain INITs) 
  490.     global kExtensionFolderType            := 'extn';    # Finder extensions go here 
  491.     global kFontsFolderType                := 'font';    # Fonts go here 
  492.     global kPreferencesFolderType        := 'pref';    # preferences for applications go here 
  493.     global kTemporaryFolderType            := 'temp';    # the Temporary Folder inside the System folder
  494. end;
  495.  
  496.  
  497.  
  498. task FindFolder( FolderType )
  499. begin
  500.     if not ( typeOf( FolderType ) = 'string' )
  501.     begin
  502.         return { errAEWrongParameters, undef, "Bad FolderType parameter" };
  503.     end;
  504.     
  505.     if ( card( FolderType ) > 4 ) or ( card( FolderType ) <= 0 ) 
  506.     begin
  507.         return { errAEWrongParameters, undef, "Bad FolderType parameter" };
  508.     end;
  509.     
  510.     x := FileTool( "FindFolder", FolderType ); 
  511.     return x;
  512. end;
  513.  
  514. task ReadVersion( FilePathName, pVersID := 1 )
  515. begin
  516.     x := FileTool( "ReadVersion", FilePathName, pVersID ); 
  517.     SErr := ScriptError();
  518.     return { x[1], x[2], x[3], SErr };
  519. end;
  520.  
  521. task ResourceExists( FilePathName, pResType, pResID )
  522. begin
  523.     x := FileTool( "ResourceExists", FilePathName, pResType, pResID ); 
  524.     SErr := ScriptError();
  525.     return { x[1], x[2], x[3], SErr };
  526. end;
  527.  
  528. task ReadSizeResource( FilePathName, pResID )
  529. begin
  530.     x := FileTool( "ReadSizeResource", FilePathName, pResID ); 
  531.     SErr := ScriptError();
  532.     return { x[1], x[2], x[3], SErr };
  533. end;
  534.  
  535.